package com.situ.student.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import javax.el.ArrayELResolver;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.synth.SynthStyle;

import com.situ.student.entity.Banji;
import com.situ.student.entity.PageBean;
import com.situ.student.entity.PageBean2;
import com.situ.student.entity.Student;
import com.situ.student.service.IBanjiService;
import com.situ.student.service.IStudentService;
import com.situ.student.service.impl.BanjiServiceImpl;
import com.situ.student.service.impl.StudentServiceImpl;

public class StudentServlet extends HttpServlet {
	private IStudentService studentService = new StudentServiceImpl();
	private IBanjiService banjiService = new BanjiServiceImpl();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// post请求乱码问题解决
		req.setCharacterEncoding("utf-8");
		// 增删改查
		// 删除
		String method = req.getParameter("method");
		if (method == null) {
			method = "pageList";
		}
		switch (method) {
		case "list":
			list(req, resp);
			break;
		// 删除
		case "deleteById":
			deleteById(req, resp);
			break;
		// 添加
		case "insert":
			insert(req, resp);
			break;
		// 更新(修改)
		case "update":
			update(req, resp);
			break;
		case "pageList":
			pageBean(req, resp);
			break;
		case "checkName":
			checkName(req, resp);
			break;
		case "toupdate":
			toupdate(req, resp);
			break;
		case "banjilist":
			banjilist(req, resp);
			break;
		default:
			break;
		}
	}

	public void banjilist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Banji> list = banjiService.list();
		System.out.println("123456789");
		req.setAttribute("list", list);
		req.getRequestDispatcher("/student_html_add.jsp").forward(req, resp);
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// 1、得到浏览器传递过来的参数信息
		Integer id = Integer.parseInt(req.getParameter("id"));
		Integer age = Integer.parseInt(req.getParameter("age"));
		String name = req.getParameter("name");
		String gender = req.getParameter("gender");
		String banjiname = req.getParameter("banjiname");
		System.out.println(banjiname);
		Student student = new Student(id, name, age, gender, banjiname);
		// 2、调用service处理
		boolean result = studentService.toupdate(student);
		System.out.println(result ? "成功" : "失败");
		// 3、重定向到列表界面
		resp.sendRedirect(req.getContextPath() + "/student?method=pageList");

	}

	public void checkName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		boolean isExist = studentService.checkName(name);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write("{\"isExist\":" + isExist + "}");
	}

	public void pageBean(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 接收数据(默认1,3, 然后根据传过来的值再更新)
		String pageNoer = req.getParameter("pageNo");
		String pageSizeer = req.getParameter("pageSize");
		System.out.println("pageNoer=" + pageNoer);
		int pageNo = 1;
		int pageSize = 3;
		if (pageNoer != null) {
			pageNo = Integer.parseInt(pageNoer);
		}
		if (pageSizeer != null) {
			pageSize = Integer.parseInt(pageSizeer);
		}
		System.out.println(pageNo);
		System.out.println(pageSize);
		// 封装成pagebean，调用service层业务逻辑
		PageBean2 pageBean = studentService.getPageBean(pageNo, pageSize);
		System.out.println("pageBean=" + pageBean);
		// 转发
		req.setAttribute("pageBean", pageBean);
		req.getRequestDispatcher("/student_html_list.jsp").forward(req, resp);

	}

	public void toupdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 根据id查找出要修改的学生对象
		Integer id = Integer.parseInt(req.getParameter("id"));
		Student student = studentService.findById(id);
		System.out.println("student=" + student);
		// 转发到student_update.jsp页面
		req.setAttribute("student", student);
		req.getRequestDispatcher("/student_toupdate.jsp").forward(req, resp);
	}

	public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Student> list = studentService.list();
		for (Student student : list) {
			System.out.println(student);
		}

		String name = req.getParameter("name");
		// if (name == null || name.equals(" ")) {
		// list = studentService.list();
		// } else {
		// // 进行模糊查找
		// list = new ArrayList<>();
		//
		// Student copyList = null;
		// Student student = copyList;
		// System.out.println(copyList);
		// list.add(copyList);
		// }
		// 将list放到req里面
		req.setAttribute("list", list);

		// 转发到页面进行展示
		req.getRequestDispatcher("/student_html_list.jsp").forward(req, resp);
	}

	public void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// 得到浏览器传递的数据
		// Integer id = Integer.parseInt(req.getParameter("id"));//强制类型转换
		String name = req.getParameter("name");
		Integer age = Integer.parseInt(req.getParameter("age"));
		String gender = req.getParameter("gender");
		Integer banjiId = Integer.parseInt(req.getParameter("banjiId"));
		Student student = new Student(name, age, gender, banjiId);
		System.out.println("student" + student);
		// 调用service处理
		boolean result = studentService.insert(student);
		System.out.println(result ? true : false);
		// 重定向刷新界面
		resp.sendRedirect(req.getContextPath() + "/student?method=pageList");
	}

	public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Integer id = Integer.parseInt(req.getParameter("id"));
		// 2.调用service处理+
		boolean result = studentService.deleteById(id);
		System.out.println(result ? true : false);
		// 重定向到列表界面(删除修改添加使用重定向，查询使用转发)
		resp.sendRedirect(req.getContextPath() + "/student?method=pageList");
	}

}
